package com.xiyou.weblog.common.domain.mapper;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiyou.weblog.common.domain.dos.StatisticsArticlePVDO;

import java.time.LocalDate;
import java.util.List;

/**
 * @author xiyou
 * @description 按日统计文章 PV 浏览量 DO
 */
public interface StatisticsArticlePVMapper extends BaseMapper<StatisticsArticlePVDO> {

    /**
     *  对指定日期的文章 PV 访问量进行 +1
     */
    default int increasePVCount(LocalDate localDate) {
        return update(null,Wrappers.<StatisticsArticlePVDO>lambdaUpdate()
                .setSql("pv_count = pv_count + 1")
                .eq(StatisticsArticlePVDO::getPvDate,localDate));
    }

    /**
     * 查询最近一周的文章 PV 访问量记录
     */
    default List<StatisticsArticlePVDO> selectLatestWeekRecord(){
        return selectList(Wrappers.<StatisticsArticlePVDO>lambdaQuery()
                .le(StatisticsArticlePVDO::getPvDate,LocalDate.now().plusDays(1))// 小于明天
                .orderByDesc(StatisticsArticlePVDO::getPvDate)
                .last("limit 7"));// 仅查询七条
    }

}
